package com.hexagram2021.sweeper_maid.command;

import com.hexagram2021.sweeper_maid.config.SMCommonConfig;
import com.hexagram2021.sweeper_maid.save.SMSavedData;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ChestMenu;
import net.minecraft.world.inventory.MenuType;
// 替换为Container接口（适配1.18+版本）
import net.minecraft.world.Container;

import javax.annotation.Nullable;

public class SMCommands {
	public static LiteralArgumentBuilder<CommandSourceStack> register() {
		return Commands.literal("sweepermaid").then(
				Commands.literal("dustbin")
						.requires(stack -> stack.hasPermission(SMCommonConfig.PERMISSION_LEVEL_DUSTBIN.get()))
						.then(Commands.argument("index", IntegerArgumentType.integer(1))
								.executes(context -> {
									int inputIndex = IntegerArgumentType.getInteger(context, "index");
									ServerPlayer player = context.getSource().getPlayer();
									if (player == null) return 0;

									int maxIndex = SMSavedData.getDustbinCount();
									if (inputIndex > maxIndex) {
										player.sendSystemMessage(Component.literal(
												"Invalid Dustbin Index! Available: 1~" + maxIndex
										));
										return 0;
									}

									int internalIndex = inputIndex - 1;
									return dustbin(player, internalIndex);
								})
						)
						.executes(context -> dustbin(context.getSource().getPlayer(), 0))
		);
	}

	private static int dustbin(@Nullable ServerPlayer player, int index) {
		if (player == null) {
			return 0;
		}

		try {
			// 使用Container接口替代IInventory
			Container targetDustbin = SMSavedData.getDustbin(index);
			int rows = Math.max(1, Math.min(targetDustbin.getContainerSize() / 9, 6));
			MenuType<?> menuType = switch (rows) {
				case 1 -> MenuType.GENERIC_9x1;
				case 2 -> MenuType.GENERIC_9x2;
				case 3 -> MenuType.GENERIC_9x3;
				case 4 -> MenuType.GENERIC_9x4;
				case 5 -> MenuType.GENERIC_9x5;
				case 6 -> MenuType.GENERIC_9x6;
				default -> MenuType.GENERIC_9x3;
			};

			player.openMenu(new MenuProvider() {
				@Override
				public Component getDisplayName() {
					return Component.literal("Dustbin " + (index + 1));
				}

				@Override
				public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player player) {
					// 传入Container类型的垃圾桶容器
					return new ChestMenu(menuType, windowId, playerInventory, targetDustbin, rows);
				}
			});
			return 1;
		} catch (IndexOutOfBoundsException e) {
			player.sendSystemMessage(Component.literal(
					"Invalid Dustbin Index! Available: 1~" + SMSavedData.getDustbinCount()
			));
			return 0;
		}
	}
}
